package com.yunxi.controller.sys;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yunxi.common.config.RedisCacheService;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.sys.pojo.MenuVO;
import com.yunxi.sys.service.MenuDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@RestController
@RequestMapping("/admin/menu")
@Api(tags = "sys:menu", description = "菜单接口 / 添加 / 删除 / 修改 / 单个读取 / 树形")
public class MenuController {
    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    MenuDubboService menuDubboService;

    @Autowired
    private RedisCacheService rcs;

    @GetMapping("/tree")
    @ApiOperation(value = "菜单树形查询")
    public ApiResult tree(@RequestParam Map<String,String> params) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        logger.debug("进入菜单列表");
        return ResponseUtil.ok(menuDubboService.findTreeListBySysType(params.get("sys_type")));
    }

    @GetMapping("/treeall")
    @ApiOperation(value = "菜单树形查询")
    public ApiResult treeall(@RequestParam Map<String,String> params) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        return ResponseUtil.ok(menuDubboService.findTreeList());
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取菜单")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "body")
    })
    public ApiResult load(@RequestParam Map<String,String> params) throws Exception {
        String id = (String)params.get("id");
        return ResponseUtil.ok("成功",menuDubboService.getById(id));
    }

    @PostMapping("/save")
    @ApiOperation(value = "菜单保存")
    public ApiResult save(@RequestBody MenuVO tmmenu) throws Exception {
        menuDubboService.saveOrUpdate(tmmenu);
        rcs.deleteKeys("menus");
        return ResponseUtil.ok("success",tmmenu);
    }

    @PostMapping("/del")
    @ApiOperation(value = "菜单删除")
    public ApiResult del(@RequestBody MenuVO tmmenu) throws Exception {
        menuDubboService.removeById(tmmenu.getMenuId());
        rcs.deleteKeys("menus:");
        return ResponseUtil.ok("success");
    }

    @GetMapping("/test")
    @ApiOperation(value = "菜单保存")
    public ApiResult test(MenuVO menu,Integer pageSize,Integer pageNum) throws Exception {
        //menuDubboService.saveOrUpdate(tmmenu);
        //rcs.deleteKeys("menu:");
        System.out.println(menu);
        System.out.println(pageSize);
        return ResponseUtil.ok("success",menu);
    }
}
